<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/10/22
 * Time: 16:01
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\common\business;

use think\Exception;
use think\facade\Log;

class Base
{
    /**
     * @param array $data
     * @param string $scene
     * @throws Exception
     */
    protected function isValidateAdd(array $data = [], string $scene = 'add')
    {
        if (!$this->valObj->scene($scene)->check($data)) {
            throw new Exception($this->valObj->getError());
        }
    }

    /**
     * @param int $limit
     * @param array $where
     * @param string $order
     * @param string $field
     * @return array
     * @throws Exception
     */
    public function getIndexLimitPage(int $limit = 10, array $where = [], string $order = "id desc", string $field = '*'): array
    {
        try {
            $array = $this->modelObj->getIndexLimitPage($limit, $where, $order, $field);
        } catch (\Exception $e) {
            Log::error("bus-user-getIndexLimitPage" . $e->getMessage() . " " . $e->getFile() . " " . $e->getLine());
            throw new Exception("查询数据失败");
        }
        return $this->getPageContent($array);
    }

    /**
     * @param array $data
     * @return array
     */
    public function getPageContent(array $data = []): array
    {
        return $data;
    }

    public function add(array $data = [])
    {
        //验证字段
        $this->isValidateAdd($data);
        //添加入库

        try {
            if (!empty($data['id'])) {
                $res = $this->modelObj->updateByData((int)$data['id'], $data);
            } else {
                $res = $this->modelObj->insertAdd($data);
            }
        } catch (\Exception $e) {
            Log::error($e->getMessage() . " " . $e->getFile() . " " . $e->getLine());
            throw new Exception("操作失败");
        }
        if ($res) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * @param string $field
     * @param string $orderBy
     * @return array
     * @throws Exception
     */
    public function getListData(string $field = '*',string $orderBy = "id desc"): array
    {
        try {
            $res = $this->modelObj->getListData($field);
        } catch (\Exception $d) {
            Log::error($d->getMessage() . " " . $d->getFile() . " " . $d->getLine());
            throw new Exception("查询失败");
        }
        return $res;
    }

    public function getBaseById(int $id = 0, string $field = '*')
    {
        try {
            $res = $this->modelObj->getBaseById($id, $field);
        } catch (\Exception $d) {
            throw new Exception("查询失败");
        }
        if (!$res) {
            return $res;
        }
        return $res->toArray();
    }

    public function deleteByData(int $id = 0)
    {
        try {
            $res = $this->modelObj->deleteByData($id);
        } catch (\Exception $d) {
            throw new Exception("查询失败");
        }
        return $res;
    }

    public function updateByData(int $id = 0, array $data = [])
    {
        try {
            $res = $this->modelObj->updateByData($id, $data);
        } catch (\Exception $d) {
            throw new Exception("查询失败");
        }
        return $res;
    }

    public function editTable(int $id = 0, string $value = "", string $field = "")
    {
        if (!$id || $id == 0) {
            throw new Exception("内部错误");
        }
        try {
            $res = $this->modelObj->updateByData($id, [
                $field => $value,
                "update_time" => time()
            ]);
        } catch (\Exception $e) {
            throw new Exception("操作失败");
        }
        return $res;
    }

    /**
     * @param int $id
     * @param int $value
     * @param string $field
     * @return mixed
     * @throws Exception
     */
    public function StatusEdit(int $id = 0, int $value = 0, string $field = "")
    {
        if (!$id || $id == 0) {
            throw new Exception("内部错误");
        }
        try {
            $res = $this->modelObj->updateByData($id, [
                $field => $value,
                "update_time" => time()
            ]);
        } catch (\Exception $e) {
            throw new Exception("操作失败");
        }
        return $res;
    }

}